/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import dao.SanphamDAO;
import java.awt.Color;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author kydrenw
 */
public class GraphSLController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("image/png");

        List<Object[]> dssp = SanphamDAO.laySanPhamBanChay(10);        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        // create the chart...       
        for(int i=0; i< dssp.size();i++)
        {
            Object[] data = dssp.get(i);
            dataset.setValue(Integer.parseInt(data[2].toString()),
                    String.valueOf(data[0]), "Sản phẩm bán chạy");            
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Các sản phẩm bán chạy", "Sản phẩm", "Số lượn bán", dataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setBackgroundPaint(Color.white);
        chart.getTitle().setPaint(Color.blue);
        CategoryPlot p = chart.getCategoryPlot();
        p.setRangeGridlinePaint(Color.red);

        ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 400, 400);
        response.getOutputStream().close();
        return null;
    }
}